/**
 @File    :   SSQ_bet_data.js
 @Contact :
 @Author  :     ypy

 @Modify Time      @Author    @Version    @Desciption
 ------------      -------    --------    -----------
 2021/2/20 14:56   ypy      1.0         查询中奖情况
 */

/******
 * 页面加载时处理函数
 * *****/
// window.onload = window_onload();//窗体加载后执行

function window_onload() {
    query_year();
    refresh_table_bet_data();
}

$(document).ready(window_onload())


/************************
 * 查询指定期号投注信息
 * **********************/
//input中输入字符后回车
function input_search_phasenum_enter(e) {
    phase_num = $("#input_search_phasenum").val().trim();

    var evt = window.event || e;
    if (evt.keyCode == 13) {
        query_one_phasenum_detail_info(phase_num)
    }
}


//查询按钮单击函数
$("#btn_query_history_by_phasenum").click(function () {
    phase_num = $("#input_search_phasenum").val().trim();//根据文本框值设置全局变量值

    if (phase_num == '') {
        alert('请确定要查询的期号')
        return
    }
    ;

    query_one_phasenum_detail_info(phase_num);
});


//指定期号查询投注情况
function query_one_phasenum_detail_info(phase_num) {

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_bet_by_phasenum", phasenum: phase_num,},
        success: function (callback) {
            history_data = callback;
            $('#table_bet_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })

}


//指定注数查询投注情况
function query_multi_phasenum_detail_info(num) {

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_bet_by_multi_phasenum", phasenum: num,},
        success: function (callback) {
            history_data = callback;
            $('#table_bet_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })

}


//查询所有中奖注数
function query_win_bet_info() {

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_win_bet_data",},
        success: function (callback) {
            history_data = callback;
            $('#table_bet_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })

}

//查询待开奖注数
function query_wait_bet_info() {

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_wait_bet_data",},
        success: function (callback) {
            history_data = callback;
            $('#table_bet_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })

}


//查询未中奖注数
function query_not_win_bet_info() {

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "btn_query_not_win_data",},
        success: function (callback) {
            history_data = callback;
            $('#table_bet_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })

}


//删除所有投注信息
function del_all_bet_data() {
    var confirm = window.confirm('将清除所有投注信息，是否继续？');
    if (!confirm) {
        return;
    }

    $.ajax({
        url: "./",
        type: "POST",
        data: {cmd_class: "del", command: "all",},//查询数据库中当前最新交易数据和交易价格
        success: function (callback) {
            refresh_table_bet_data();
        },
        error: function () {
            alert('删除失败');
        }

    });
}


//删除选中投注信息
function del_select_bet_data(e) {
    var rows = $("#table_bet_data").bootstrapTable('getAllSelections');
    //获取所有选中股票的ts_code
    var select_list = [];
    var phase_list = [];
    for (var i = 0; i < rows.length; i++) {
        select_list.push(rows[i].id);
        phase_list.push(rows[i].phasenum);
    }
    if (select_list.length == 0) {
        alert('请选择需要删除的投注');
        return;
    }

    var confirm = window.confirm('是否确认删除选中投注期数！！！\n' + phase_list);
    if (!confirm) {
        return;
    }

    $.ajax({
        url: "./",
        type: "POST",
        data: {cmd_class: "del", command: "selectlist", selectlist: JSON.stringify(select_list)},//查询数据库中当前最新交易数据和交易价格
        success: function (callback) {
            refresh_table_bet_data()
        },
        error: function () {
            alert('删除失败');
        }

    });
}


//select选择后进行查询
$("#select_bet_num").change(function () {

    select_num = $("#select_bet_num option:selected").text();
    // query_multi_phasenum_detail_info(parseInt(select_num));
    query_bet_by_year(select_num);
});


/*************
 * 表格相关函数
 *************/
//刷新表格
function refresh_table_bet_data() {
    get_bet_data();
    init_table_bet_data();

}

//获取历史投注信息
function get_bet_data() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_bet_data"},
        success: function (callback) {
            bet_data = callback;
            $('#table_bet_data').bootstrapTable("load", bet_data);
        },
        error: function () {
            alert('无法获取投注数据，请检查网络连接');
        }
    })
}

//初始化开奖信息表格函数
function init_table_bet_data() {

    $('#table_bet_data').bootstrapTable("destroy");//  清除原有数据
    $('#table_bet_data').bootstrapTable({
        // height: 800,
        locale: "zh-CN",
        columns: [
            {
                title: '选中',
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            }, {
                title: 'id',
                field: 'id',
                align: 'center',
                valign: 'middle',
                sortable: true,
                visible: false,
                // formatter: ts_code_link_Formatter,
            },
            {
                title: '期号',
                field: 'phasenum',
                align: 'center',
                valign: 'middle',
                sortable: true,
                formatter: phasenum_Formatter,
            }, {
                field: 'red1',
                title: '红球1',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red2',
                title: '红球2',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red3',
                title: '红球3',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red4',
                title: '红球4',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red5',
                title: '红球5',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red6',
                title: '红球6',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'blue',
                title: '蓝球',
                align: 'center',
                valign: 'middle',
                formatter: blueball_Formatter,
            }, {
                title: '中奖等级',
                field: 'bonuslevel',
                align: 'center',
                valign: 'middle',
            }, {
                field: 'multiple',
                title: '倍数',
                align: 'center',
                valign: 'middle',
            }, {
                field: 'additonal_bet',
                title: '追加',
                align: 'center',
                valign: 'middle',
                visible: false,

            }, {
                field: 'bonusprice',
                title: '中奖金额',
                align: 'right',
                valign: 'middle',
                footerFormatter: function (value) {
                    var sum_bet_price = 0;
                    var sum_bonus = 0;
                    var additonal_bet = 1;
                    for (var i in value) {
                        if (value[i].additonal_bet == true) {
                            additonal_bet = 1.5
                            sum_bet_price += 2 * additonal_bet * parseInt(value[i].multiple)
                        } else {
                            additonal_bet = 1
                            sum_bet_price += 2 * additonal_bet * parseInt(value[i].multiple)
                        }
                        sum_bonus += parseInt(value[i].bonusprice);

                    }
                    return '共投注' + sum_bet_price + '元,奖金' + sum_bonus + '元';
                }
            },
            // {
            //     field: 'is_end',
            //     title: '关注',
            //     align: 'center',
            //     formatter: operateFormatter,
            //     // clickToSelect: true,
            // },

        ],
    });
}

//红球红色显示
function redball_Formatter(value, row, index) {

    var a = '<span style="color: red">' + value + '</span>';

    return a;
}

//蓝球蓝色显示
function blueball_Formatter(value, row, index) {

    var a = '<span style="color: #007bff">' + value + '</span>';

    return a;
}

//格式化期号数据，添加鼠标移动
function phasenum_Formatter(value, row, index) {
    return [
        "<a href='#' onclick='show_phase_data(this)'>" + value + "</a>"
    ].join('');
}


//移动到期号上时显示当期开奖数据
function show_phase_data(param) {
    phasenum = param.innerHTML;


}


//添加年份查询选项
function query_year() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "year"},
        success: function (callback) {
            add_option_to_select_bet_num(callback);

        },
        error: function () {

        }
    })
}

//向select中添加年度数据
function add_option_to_select_bet_num(result) {
    $("#select_bet_num").empty();
    for (i = 0; i < result.length; ++i) {
        var option = "<option  label=\'" + result[i] + "\'  value=\'" + result[i] + "\'  >" + result[i] + "</option>";//datalist中label时最终显示信息，value是用来查找的，chromn可以同时按两个属性查找
        // var option = "<option value=\'"+result[0][i]+"\'  />";

        $("#select_bet_num").append(option);
    }
    var option = "<option  label=\'all\'  value=\'all\'  >" + "all" + "</option>";
    $("#select_bet_num").append(option);
}


//根据年份查询数据
function query_bet_by_year(year) {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_bet_by_year", year: year,},
        success: function (callback) {
            history_data = callback;
            $('#table_bet_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })
}